﻿using Jy.Abp.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.PostgreSql;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace AbpProjectTemplate.EntityFrameworkCore;

[DependsOn(
	typeof(AppDomainModule),
	typeof(JyAbpGeneralTreeEntityFrameworkCoreModule),
	typeof(AbpIdentityEntityFrameworkCoreModule),
	typeof(AbpOpenIddictEntityFrameworkCoreModule),
	typeof(AbpPermissionManagementEntityFrameworkCoreModule),
	typeof(AbpSettingManagementEntityFrameworkCoreModule),
	typeof(AbpEntityFrameworkCorePostgreSqlModule),
	typeof(AbpBackgroundJobsEntityFrameworkCoreModule),
	typeof(AbpAuditLoggingEntityFrameworkCoreModule),
	typeof(AbpTenantManagementEntityFrameworkCoreModule),
	typeof(AbpFeatureManagementEntityFrameworkCoreModule)
	)]
public class AppEntityFrameworkCoreModule : AbpModule
{
	public override void PreConfigureServices(ServiceConfigurationContext context)
	{
        AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
        AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);

        AppEfCoreEntityExtensionMappings.Configure();
	}

	public override void ConfigureServices(ServiceConfigurationContext context)
	{
		context.Services.AddAbpDbContext<AppDbContext>(options =>
		{
			/* Remove "includeAllEntities: true" to create
			 * default repositories only for aggregate roots */
			options.AddDefaultRepositories(includeAllEntities: true);
            options.AddGeneralTreeRepositories();
        });

		Configure<AbpDbContextOptions>(options =>
		{
			/* The main point to change your DBMS.
			 * See also AbpProjectTemplateMigrationsDbContextFactory for EF Core tooling. */
			options.UseNpgsql();
		});

		//context.Services.AddTransient(typeof(IGeneralTreeRepository<,>), typeof(EfCoreGeneralTreeRepository<,>));
	}
}
